package com.yuexiao.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuexiao.entity.ResultInfo;
import com.yuexiao.pojo.Orders;
import com.yuexiao.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName:OrderController
 * @Auther: YUEXIAO
 * @Description: 订单操作控制器
 * @Date: 2022/8/15 14:20
 * @Version: v1.0
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Reference
    private OrdersService ordersService;




    /**
     * 提交订单
     * @param orders Orders
     * @return ResultInfo 返回结果对象
     */
    @PostMapping("/submit")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo submit(@RequestBody Orders orders){
        log.info("订单数据:{}",orders);
        Orders getOrders = ordersService.insertOrders(orders);
        if(getOrders==null){
            return ResultInfo.error("操作失败");
        }

        //封装请求参数


        return ResultInfo.success(null);
    }


    /**
     * 分页查询用户订单信息
     * @param page
     * @param pageSize
     * @param request
     * @return
     */
    @GetMapping("/orderPaging")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo orderPaging(int page, int pageSize, HttpServletRequest request){
        Object userid = request.getSession().getAttribute("userid");
        if (userid == null) {
            return ResultInfo.error("用户未登录");
        }
        return ResultInfo.success(ordersService.getOrdersByPage(Long.valueOf(String.valueOf(userid)),page,pageSize));
    }

}
